package com.server.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.server.service.ICommonDataService;
import com.server.service.IUserConfigService;

@Service("UserConfigService")
public class UserConfigService implements IUserConfigService {
	
	private static final String TABLE_NAME = "reward.user_config";
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDataService;
	
	/**
	 * 属性配置新增
	 * @param operator
	 * @param inputMap
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月9日
	 */
	@Override
	public Boolean add(String fieldName, String content) {
		Integer isLong = 0;
		if (fieldName.contains("Note")) {
			isLong = 1;
		}
		String sql = "insert into %s (FieldName,Content,isLong) values('%s','%s',%s)";
		return jdbcTemplate.update(String.format(sql, TABLE_NAME,fieldName,content,isLong)) > 0;
	}
	@Override
	public Boolean update(String id, String fieldName, String content) {
		if (StringUtils.isEmpty(fieldName) && StringUtils.isEmpty(content)) {
			return false;
		}
		Integer isLong = 0;
		if (fieldName.contains("Note")) {
			isLong = 1;
		}
		StringBuffer sqlBuffer = new StringBuffer();
		sqlBuffer.append("update ").append(TABLE_NAME).append(" set ");
		if (!StringUtils.isEmpty(fieldName)) {
			sqlBuffer.append("FieldName='").append(fieldName).append("'");
			if (!StringUtils.isEmpty(content)) {
				sqlBuffer.append(" and Content='").append(content).append("'");
			}
		}else {
			sqlBuffer.append("Content='").append(content).append("'");
		}
		sqlBuffer.append(" and IsLong=").append(isLong);
		return jdbcTemplate.update(sqlBuffer.toString()) > 0;
	}
	
	@Override
	public Boolean delete(String ids){
		String sql = "Delete from %s where Id in (%s)";
		return jdbcTemplate.update(String.format(sql, TABLE_NAME,ids))>0;
	}

	@Override 
	public List<Map<String, Object>> getList(){
		String sql = "select * from %s where Valid=1 order by IsLong asc,Id asc";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, TABLE_NAME));
		return list;
	}
	
	 /**
	 * 获取可用的扩展字段
	 * @param type
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月9日
	 */
	@Override 
	public List<Map<String, Object>> getFields(Integer type){
		List<Map<String, Object>> returnList = new ArrayList<Map<String,Object>>();
		String sql = "select * from %s where Valid=1 and IsLong=%s order by Id";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, TABLE_NAME, type));
		Iterator<Map<String, Object>> iterator = list.iterator();
		StringBuffer sBuffer = new StringBuffer();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			if (sBuffer.length() > 0) {
				sBuffer.append(",");
			}
			sBuffer.append(next.get("FieldName"));
		}
		String fields = sBuffer.toString();
		if (type.equals(0)) {
			for(int i=1; i<=20; i++){
				String tempStr = "Extend"+i;
				if (!fields.contains(tempStr)) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("FieldName", tempStr);
					returnList.add(map);
				}
			}
		}else {
			for(int i=1; i<=10; i++){
				String tempStr = "Note"+i;
				if (!fields.contains(tempStr)) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("FieldName", tempStr);
					returnList.add(map);
				}
			}
		}
		return returnList;
	}

	@Override
	public Map<String, Object> getForm(String id) {
		return commonDataService.getFormData(TABLE_NAME, "Id", id, null, null);
	}
	
}
